package com.lagou.edu.service.impl;

import com.lagou.edu.dao.LagouAuthTokenDao;
import com.lagou.edu.pojo.LagouAuthToken;
import com.lagou.edu.service.CodeService;
import com.lagou.edu.service.EmailService;
import com.lagou.edu.util.CodeUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

@Service
public class CodeServiceImpl implements CodeService {
    @Autowired
    private LagouAuthTokenDao lagouAuthTokenDao;

    @Reference(check = false)
    private EmailService emailService;

    @Override
    public Boolean getCode(String email) {
        String code = CodeUtil.getCode();
        LagouAuthToken lagouAuthToken = new LagouAuthToken();
        lagouAuthToken.setEmail(email);
        lagouAuthToken.setCode(code);
        Date date = new Date();
        lagouAuthToken.setCreatetime(date);
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(Calendar.MINUTE, 10);
        Date expireTime = instance.getTime();
        lagouAuthToken.setExpiretime(expireTime);
        lagouAuthTokenDao.save(lagouAuthToken);
        emailService.sendSimpleMail(email,"注册用户的验证码" , code);
        return true;
    }


}
